<?php
/*
*
* @author ram
*
*/
error_reporting(15);
set_time_limit(0);
define('DEBUG',false);
define("FSOCK_ENABLED", true);
define("TIME_ELAPSED_MAX", 600); //10 mins
define("LINKS_FOUND_MAX", 500);


require_once("urlreader.php");
require_once("linkchecker.php");
require_once("filehandle.php");
require_once("functions.php");

$f_handle = new filehandle();
$f_handle->is_able_write();

$allow_url_fopen = ini_get('allow_url_fopen');
if (!$allow_url_fopen) {
	ini_set('allow_url_fopen', 1);
}

$allow_url_fopen = ini_get('allow_url_fopen');
if (!$allow_url_fopen && !FSOCK_ENABLED ) {
	echo '<span class="error"><b>Note</b>: Please enable \'<em>allow_url_fopen</em>\' option in PHP configuration file</span><br/>';
}

$search_url = trim(@$_POST['search_url']);
$search_page = trim(@$_POST['search_page']);
$fxid = trim(@$_POST['fxid']);

if (empty($search_url) && empty($fxid) ) {
	header("Location: index.php");exit;
}

if (!empty($search_url) && empty($fxid) ) {
	$fxid = md5($search_url);

	$obj = new linkchecker($search_url);
	$GLOBALS['URL_INFO']['BASE'] = $obj->get_base();
	$search_page = $obj->get_full_url($search_url);

	$GLOBALS['URL_INFO']['STARTTIME'] = $_POST['starttime'];

	$GLOBALS['URL_INFO']['ROBOTS'] = get_robots($GLOBALS['URL_INFO']['BASE']);

	$read_all_links = false;
} else {

	$GLOBALS['URL_INFO'] = $f_handle->read_data($fxid);

	$obj = new linkchecker($search_page);

	$read_all_links = true;
}

$search_page = $obj->remove_base($search_page);
$full_url = $obj->get_url_with_base($GLOBALS['URL_INFO']['BASE'],$search_page);

if (empty($GLOBALS['URL_INFO']['LINKS'])) $GLOBALS['URL_INFO']['LINKS'] = array();
$u_handle = new urlreader();
$checker = new linkchecker($full_url);

//Read Data
list($headers, $htmlCode) = $u_handle->read_data($full_url, $search_page);

//do somthing with headers
$checker->modify_headers($search_page, $headers); 
//do somthing with headers

$anchors = get_html_tags($htmlCode, 'a');
$title_tag = get_html_tags($htmlCode, 'title');

$well_links = $checker->get_links($anchors, $title_tag[0]['text'], $read_all_links);

$GLOBALS['URL_INFO']['LINKS'] = array_merge($GLOBALS['URL_INFO']['LINKS'], $well_links);
$f_handle->write_data($fxid, $GLOBALS['URL_INFO'] );
//echo $full_url, $search_page; die;

if(empty($_POST['get_status']) ) 
{

	$js = '[';
	foreach ($GLOBALS['URL_INFO']['LINKS'] as $link => $link_info) {
		if (!empty($link))
			$js .= "'".addslashes($link)."',";
	}
	$js .= '\'\']';

	require_once("header.php");
	echo '<table width="98%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td class="lebel2"><script language="JavaScript" src="load.js"></script></td>
	  </tr>
	  <tr>
		<td class="">&nbsp;</td>
	  </tr><tr>
		<td align="left" valign="top">';
	echo '<div id="link_status"></div>';
	echo '</td></tr>
		 <tr>
			<td align="left">
			 <table width="80%" border="0" cellspacing="2" cellpadding="0">
			 <tr>
			 <td width="20">&nbsp;</td>
			 <td width="100" align="left" class="dataField">Time elapsed: </td>
			 <td width="10">&nbsp;</td>
			 <td align="left" class="dataLabel"><div id="txt"></div><script>startTime();</script></td>
			</tr>
			</table></td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
		  </tr>
		</table></td>
	  </tr>
	  <tr>
		<td>&nbsp;</td>
	  </tr>
	</table>';

	echo '
	<script>
	var total = '.count($GLOBALS['URL_INFO']['LINKS']).';
	var fxid = \''.$fxid.'\';
	var links = '.$js.';
	var ccount = 0;
	var response_text = \'\';
	var TOTAL_TIME_MAX = '.TIME_ELAPSED_MAX.';

	var xmlhttp_config = {"URL":"sitemap.php","METHOD":"POST","ASYNCH":true}
	var xmlHRH = new xmlHttpRequestHandler(xmlhttp_config);

	function doCommand(ob) {
		//if (ccount <= 0)
			//alert(ob.responseText);
		if(ob.responseText != \'\') {
			try{
				//document.getElementById("link_status").innerHTML = ob.responseText;
				//ob.inputHandler.innerHTML = ob.responseText;
				response_text = ob.responseText;
				ccount++;
				if (ccount >= total)
					window.location = "results.php?fxid="+fxid;
				alwaysDOIT();
				//setTimeout("alwaysDOIT()", 3000);
			} catch(e) {
			}
		}
		//alert(ob.getAllResponseHeaders());
	}

	function alwaysDOIT() {
		if (ccount == 0) {
			xmlHRH.inputHandler = document.getElementById("link_status");
		}
		var d = new Date();
		var t = d.getTime();
		document.getElementById("link_status").innerHTML = response_text; //"Processing "+links[ccount]+"...<br><br>"+response_text;
		xmlHRH.setData("fxid="+fxid+"&search_page="+links[ccount]+"&get_status=true"+"&timenow="+t);
		xmlHRH.send(doCommand);
	}

	alwaysDOIT();
	</script>';

	require_once("footer.php");
} else {
	/*
	echo 'Found:'.count($GLOBALS['URL_INFO']['LINKS']).' Links<br>';
	echo 'Searched on:'.$search_page.'<br>';
	echo 'Headers:';
	debug($headers);
	*/
	$time_elapsed = ( $_POST['timenow'] - $GLOBALS['URL_INFO']['STARTTIME'] )/ ( 1000 * 60 ) ;
	
	$time_elapsed = round($time_elapsed,2) . ' Mins'; // . ($time_elapsed % 60) .' Secs' ;
	
	echo '<table width="80%" border="0" cellspacing="2" cellpadding="0">
		  <tr>
			<td width="20">&nbsp;</td>
			<td width="100" align="left" class="dataField">Current page:</td>
			<td width="10">&nbsp;</td>
			<td align="left" class="dataLabel">'.$search_page.'</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			<td align="left" class="dataField">Pages found:</td>
			<td>&nbsp;</td>
			<td align="left" class="dataLabel">'.count($GLOBALS['URL_INFO']['LINKS']).'<script>var TOTAL_LINKS_MAX = '.LINKS_FOUND_MAX.';
		  var LINKS_FOUND_MAX = '.count($GLOBALS['URL_INFO']['LINKS']).';</script></td>
		  </tr>
		  <!--<tr>
			<td>&nbsp;</td>
			<td align="left" class="dataField">Pages left:</td>
			<td>&nbsp;</td>
			<td align="left" class="dataLabel">&nbsp;</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			<td align="left" class="dataField">Time passed:</td>
			<td>&nbsp;</td>
			<td align="left" class="dataLabel">'.$time_elapsed.'</td>
		  </tr>--></table>
		  ';
	
}

?>